package com.rbw.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rb.util.NumUtil;
import com.rb.util.StringUtil;
import com.rbw.controller.req.TechParam;
import com.rbw.controller.res.BaseResult;
import com.rbw.controller.res.ListResult;
import com.rbw.controller.res.model.RTechBean;
import com.rbw.model.IndustryBean;
import com.rbw.model.OriginBean;
import com.rbw.model.TechBean;
import com.rbw.model.TechCategoryBean;
import com.rbw.model.TradeTypeBean;
import com.rbw.service.TechService;
import com.rbw.service.exception.ServiceException;

@Controller
@RequestMapping("/tech")
public class TechController {

	@Autowired
	TechService techServiceImpl;

	@RequestMapping("/v1/getLists")
	@ResponseBody
	public ListResult<RTechBean> getLists(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "category[]", required = false) List<String> categories,
			@RequestParam(value = "tradetype[]", required = false) List<String> tradetypes,
			@RequestParam(value = "industry[]", required = false) List<String> industries,
			@RequestParam(value = "origin[]", required = false) List<String> origins) {
		String sessionId = request.getSession().getId();
		ListResult<RTechBean> result = new ListResult<RTechBean>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sort = !StringUtil.checkEmpty(request.getParameter("sort"))
				? StringUtil.toString(request.getParameter("sort"), null) : "releaseTime";// 按时间排序
		String desc = !StringUtil.checkEmpty(request.getParameter("desc"))
				? StringUtil.toString(request.getParameter("desc"), null) : null;
		String content = !StringUtil.checkEmpty(request.getParameter("content"))
				? StringUtil.toString(request.getParameter("content"), null) : null;// 搜索内容
		int page = NumUtil.toInt(request.getParameter("page"), 1);
		int pageSize = NumUtil.toInt(request.getParameter("pageSize"), 10);
		ListResult<TechBean> res = techServiceImpl.getLists(sessionId, page, pageSize, origins, industries, categories, tradetypes,
				sort, desc, content);
		if (res != null && res.getData() != null) {
			List<RTechBean> list = new ArrayList<>();
			for (TechBean tech : res.getData()) {
				RTechBean rt = new RTechBean();
				rt.setTid(tech.getTid());
				rt.setName(tech.getName());
				rt.setAddress(tech.getAddress());
				rt.setReleaseTime(sdf.format(tech.getReleaseTime()));
				rt.setLabels(tech.getLabels());
				rt.setOrigin(tech.getOrigin());
				rt.setIndustry(tech.getIndustry());
				rt.setCategory(tech.getCategory());
				rt.setTradeType(tech.getTradeType());
				rt.setContent(tech.getContent());
				rt.setFavoriteState(tech.getFavoriteState());
				list.add(rt);
			}
			result.setData(list);
			result.setTotalSize(res.getTotalSize());
		}
		return result;
	}

	/**
	 * 获取用户关注技术成果列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/v1/getUserTechs")
	@ResponseBody
	public ListResult<RTechBean> getUserTechs(HttpServletRequest request, HttpServletResponse response) {
		String sessionId = request.getSession().getId();
		ListResult<RTechBean> result = new ListResult<RTechBean>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int page = NumUtil.toInt(request.getParameter("page"), 1);
		int pageSize = NumUtil.toInt(request.getParameter("pageSize"), 10);
		ListResult<TechBean> res = techServiceImpl.getUserTechs(sessionId, page, pageSize);
		if (res != null && res.getData() != null) {
			List<RTechBean> list = new ArrayList<>();
			for (TechBean tech : res.getData()) {
				RTechBean rt = new RTechBean();
				rt.setTid(tech.getTid());
				rt.setName(tech.getName());
				rt.setAddress(tech.getAddress());
				rt.setReleaseTime(sdf.format(tech.getReleaseTime()));
//				rt.setLabels(tech.getLabels());
				rt.setOrigin(tech.getOrigin());
				rt.setIndustry(tech.getIndustry());
				rt.setCategory(tech.getCategory());
				rt.setTradeType(tech.getTradeType());
				rt.setContent(tech.getContent());
				rt.setFavoriteState(tech.getFavoriteState());
				list.add(rt);
			}
			result.setData(list);
			result.setTotalSize(res.getTotalSize());
		}
		return result;
	}
	
	/**
	 * 获取行业信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/v1/getIndustries")
	@ResponseBody
	public ListResult<IndustryBean> getIndustries(HttpServletRequest request, HttpServletResponse response) {
		ListResult<IndustryBean> result = new ListResult<IndustryBean>();
		List<IndustryBean> industries = techServiceImpl.getIndustries();
		result.setData(industries);
		result.setTotalSize(industries.size());
		return result;
	}

	/**
	 * 获取技术来源信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/v1/getOrigins")
	@ResponseBody
	public ListResult<OriginBean> getOrigins(HttpServletRequest request, HttpServletResponse response) {
		ListResult<OriginBean> result = new ListResult<OriginBean>();
		List<OriginBean> origins = techServiceImpl.getOrigins();
		result.setData(origins);
		result.setTotalSize(origins.size());
		return result;
	}
	
	/**
	 * 获取技术分类信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/v1/getCategories")
	@ResponseBody
	public ListResult<TechCategoryBean> getCategories(HttpServletRequest request, HttpServletResponse response) {
		ListResult<TechCategoryBean> result = new ListResult<TechCategoryBean>();
		List<TechCategoryBean> categories = techServiceImpl.getCategories();
		result.setData(categories);
		result.setTotalSize(categories.size());
		return result;
	}
	
	/**
	 * 获取交易方式信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/v1/getTradeTypes")
	@ResponseBody
	public ListResult<TradeTypeBean> getTradeTypes(HttpServletRequest request, HttpServletResponse response) {
		ListResult<TradeTypeBean> result = new ListResult<TradeTypeBean>();
		List<TradeTypeBean> types = techServiceImpl.getTradeType();
		result.setData(types);
		result.setTotalSize(types.size());
		return result;
	}
	
	/**
	 * 保存科技技术信息
	 * 
	 * @param param
	 * @param error
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/v1/saveTech")
	@ResponseBody
	public BaseResult saveTech(@Validated TechParam param, BindingResult error, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		boolean flag = false;
		if (error.hasErrors()) {
			result.setFlag(false);
			result.setMsg(error.getFieldError().getDefaultMessage());
		}
		
		try {
			String sessionId = request.getSession().getId();
			flag = techServiceImpl.saveTech(sessionId, param);
			result.setFlag(flag);
			result.setRescode(flag?BaseResult.success.getCode():BaseResult.fail.getCode());
			result.setMsg(flag?BaseResult.success.getMsg():BaseResult.fail.getMsg());
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

}
